<?php
/**
 *
 * 发送到购物车
 *
 * @version        $Id: posttocar.php 1 15:38 2010年7月8日Z tianya $
 * @package        DedeCMS.Site
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once (dirname(__FILE__) . "/../include/common.inc.php");
require_once DEDEINC.'/shopcar.class.php';
$cart = new MemberShops();
$product1 = $cart->getItems();

$do = isset($do) ? trim($do) : 'add';
$product = $cart->getItems();
if($do == 'add')
{
    /*
    function addItem();                add a product to car
    */
    /*$buynum = isset($buynum) && is_numeric($buynum) ? $buynum : 1;
	$attr_color = isset($attr_color) && trim($attr_color) ? $attr_color : '';
	$attr_volume  = isset($attr_volume ) && trim($attr_volume ) ? $attr_volume  : '';
    $id =empty($id)? "" : intval($id);
    $buynum = ($buynum < 1) ? 1 : $buynum;
    $rs = $dsql->GetOne("SELECT id,channel,title FROM #@__archives WHERE id='$id'");
    if(!is_array($rs))
    {
        ShowMsg("该商品已不存在！","-1");
        exit();
    }
    $cts = GetChannelTable($rs['channel']);
    $rows = $dsql->GetOne("SELECT aid as id,trueprice as price,units FROM `$cts[addtable]` WHERE aid='$id'");
    if(!is_array($rows))
    {
        ShowMsg("该商品已不存在！","-1");
        exit();
    }
    $rows['buynum'] = $buynum;
	$rows['attribute'] = $attr_color."<br/>".$attr_volume;
    $rows['title']     = $rs['title'];
    $cart->addItem($id, $rows);*/
    ShowMsg("已添加加到购物车,<a href='car.php'>查看购物车</a>","car.php");
    exit();
    
}elseif($do == 'ajaxadd')
{
    /*
    function addItem();                add a product to car
    */
	AjaxHead();
    $buynum = isset($buynum) && is_numeric($buynum) ? $buynum : 1;
    $attr_price = 0;
	$attr_color_value = isset($attr_color) && $attr_color ? trim(substr($attr_color,0,strpos($attr_color,'$'))) : '';
	$attr_volume_value  = isset($attr_volume ) && $attr_volume  ? trim(substr($attr_volume,0,strpos($attr_volume,'$')))  : '';
	$attr_volume_value && $attr_price = intval(substr($attr_volume,(strpos($attr_volume,'$')+1)));
    $id =empty($id)? "" : intval($id);
    $buynum = ($buynum < 1) ? 1 : $buynum;
    $rank = 0;
    $result = array();
    $rs = $dsql->GetOne("SELECT id,channel,title,brand_id FROM #@__archives WHERE id='$id'");
    if(!is_array($rs))
    {
        $result['error'] = 1;
        exit();
    }
    if ($rs['brand_id']){
    	$res = $dsql->GetOne("SELECT rank FROM #@__arctype WHERE id='$rs[brand_id]'");
    	if (is_array($res)){
    		$rank = $res['rank'];
    	}
    }
    $cts = GetChannelTable($rs['channel']);
    $rows = $dsql->GetOne("SELECT aid as id,trueprice as price,units FROM `$cts[addtable]` WHERE aid='$id'");
    if(!is_array($rows))
    {
         $result['error'] = 1;
         exit();
    }
    $rows['buynum'] = $buynum;
	$rows['attribute'] = $attr_color_value."<br/>".$attr_volume_value;
    $rows['title']     = $rs['title'];
    $attr_price && $rows['price'] = $attr_price; 
    $rows['price'] = $rank ? $rows['price']*$rank : $rows['price'];
    $id = $id."_".md5($rows['attribute']);
    $rows['cid'] = $id;
    $product = $cart->getItems();
    if (isset($product[DE_ItemEcode.$id])){
    	$bArr = $product[DE_ItemEcode.$id];
    	$rows['buynum'] += $bArr['buynum'] ;
    }
    $totalCount = 0;
    if(!empty($product)){
	    foreach ($product as $p){
	    	$totalCount += $p['buynum']*$p['price'];
	    	$buytotal += $p['buynum'];
	    }
    }
    $cart->addItem($id, $rows);
    $totalCount += $buynum*$rows['price'];
    $result['buynum'] = $buytotal+$buynum;
    $result['error'] = 0;
    $result['totalCount'] = $totalCount;
    echo json_encode($result);
    exit();
}
elseif($do == 'del')
{
    /*
    function delItem();                del products from car
    */
    if(!isset($ids))
    {
        ShowMsg("请选择要删除的商品！","-1");
        exit;
    }
    if(is_array($ids))
    {
        foreach($ids as $id)
        {
            $id = $id;
            $cart->delItem($id);
        }
    }
    else
    {
        $ids = $ids;
        $cart->delItem($ids);
    }
    ShowMsg("已成功删除购物车中的商品,<a href='car.php'>查看购物车</a>","car.php");
    exit;
}
elseif($do == 'clear')
{
    /*
    function clearItem();        clear car products all!
    */
    $cart->clearItem();
    ShowMsg("购物车中商品已全部清空！","car.php");
    exit;
}
elseif($do == 'update')
{
    /*
    function updateItem();        update car products number!
    */
    if(isset($ids) && is_array($ids))
    {
        foreach($ids as $id){
            $id = intval($id);
            $rs = $dsql->GetOne("SELECT id,channel,title FROM #@__archives WHERE id='$id'");
            if(!is_array($rs)) continue;
            $cts = GetChannelTable($rs['channel']);
            $rows = $dsql->GetOne("SELECT aid as id,trueprice as price,units FROM `$cts[addtable]` WHERE aid='$id'");
            if(!is_array($rows)) continue;
            $rows['buynum'] = intval(${'buynum'.$id});
            if($rows['buynum'] < 1)
            {
                //如果设单位数量小于1个时更新,则移出购物车
                $cart->delItem($id);
                continue;
            }
            $rows['title']     = $rs['title'];
            $cart->addItem($id, $rows);
        }
    }
    ShowMsg("购物车中商品已全部更新！","car.php");
    exit;
}